#!/bin/sh /etc/rc.common
# Copyright (C) 2016 OpenWrt.org

START=45

boot() {
    [ -s /etc/.app_store.id ] && return 0

    mkdir -p /tmp/is-inst/var/lock
    mkdir -p /tmp/is-inst/etc
    cp -a /etc/opkg.conf /tmp/is-inst/etc/
    cp -a /etc/opkg /tmp/is-inst/etc/
    ARCH=`opkg -f /tmp/is-inst/etc/opkg.conf --offline-root /tmp/is-inst print-architecture | grep '^arch ' | grep -v 'arch all' | grep -v 'arch noarch' | sed -E 's/^arch ([^ ]+) (\d+)$/\2 \1/g' | sort -n -r | head -1 | cut -d ' ' -f2`
    rm -rf /tmp/is-inst

    for iface in eth0 br-lan; do
        if [ -e /sys/class/net/$iface/address ]; then
            HASH=`md5sum /sys/class/net/$iface/address | cut -d ' ' -f1`
            break
        fi
    done

    if [ -z "$HASH" ]; then
        HASH=`dd if=/dev/urandom bs=512 count=1 2>/dev/null | md5sum | cut -d ' ' -f1`
    fi

    echo "{\"arch\":\"${ARCH}\", \"uid\":\"${HASH}\"}" > /etc/.app_store.id
}
